<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: downloads.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once (DIR_FS_INC.'inc.date_long.php');

$module_smarty = new Smarty;

if(!strstr($PHP_SELF, 'account_history_info')) {
	$orders = $db->db_query("SELECT 
									orders_id, 
									orders_status 
								FROM 
									".TABLE_ORDERS." 
								WHERE 
									customers_id = '".$_SESSION['customer_id']."' 
								ORDER BY 
									orders_id desc limit 1");
	
	$last_order = $orders->fields['orders_id'];
	$order_status = $orders->fields['orders_status'];
	
} else {
	$last_order = (int)$_GET['order_id'];
	$order_status = $db->db_query("SELECT orders_status FROM ".TABLE_ORDERS." WHERE orders_id = '".$last_order."'");
	$order_status = $orders->fields['orders_status'];
}

if ($order_status < DOWNLOAD_MIN_ORDERS_STATUS)
	$module_smarty->assign('dl_prevented', 'true');

$downloads = $db->db_query("SELECT 
								DATE_FORMAT(o.date_purchased, '%Y-%m-%d') AS date_purchased_day, 
								opd.download_maxdays, 
								op.products_name, 
								opd.orders_products_download_id, 
								opd.orders_products_filename, 
								opd.download_count, 
								opd.download_maxdays 
							FROM 
								".TABLE_ORDERS." o, 
								".TABLE_ORDERS_PRODUCTS." op, 
								".TABLE_ORDERS_PRODUCTS_DOWNLOAD." opd 
							WHERE 
								o.customers_id = '".$_SESSION['customer_id']."' 
							AND 
								o.orders_id = '".$last_order."' 
							AND 
								o.orders_id = op.orders_id 
							AND 
								op.orders_products_id = opd.orders_products_id 
							AND 
								opd.orders_products_filename != ''");
									
if($downloads->_numOfRows) {
	$jj = 0;

	while(!$downloads->EOF) {
		list($dt_year, $dt_month, $dt_day) = explode('-', $downloads->fields['date_purchased_day']);
		$download_timestamp = mktime(23, 59, 59, $dt_month, $dt_day + $downloads->fields['download_maxdays'], $dt_year);
		$download_expiry = date('Y-m-d H:i:s', $download_timestamp);
		if (($downloads->fields['download_count'] > 0) && (file_exists(DIR_FS_DOWNLOAD.$downloads->fields['orders_products_filename'])) && (($downloads->fields['download_maxdays'] == 0) || ($download_timestamp > time())) && ($order_status >= DOWNLOAD_MIN_ORDERS_STATUS)) {
			$dl[$jj]['download_link'] = '<a href="'.href_link(FILENAME_DOWNLOAD, 'order='.$last_order.'&id='.$downloads->fields['orders_products_download_id']).'">'.$downloads->fields['products_name'].'</a>';
			$dl[$jj]['pic_link'] = href_link(FILENAME_DOWNLOAD, 'order='.$last_order.'&id='.$downloads->fields['orders_products_download_id']);
			
		} else
			$dl[$jj]['download_link'] = $downloads->fields['products_name'];
			
		$dl[$jj]['date'] = date_long($download_expiry);
		$dl[$jj]['count'] = $downloads->fields['download_count'];
		$jj ++;
		$downloads->MoveNext();
	}
}
$module_smarty->assign('dl', $dl);
$module_smarty->assign('language', $_SESSION['language']);
$module_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
$module_smarty->caching = false;
$module = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/downloads.html');
$smarty->assign('downloads_content', $module);
?>